import {WeaDialog, WeaTextarea} from 'ecCom';
import {Button, Modal, Input} from 'antd';
import * as Apis from '../../../../apis/previewMeeting';
import {WeaLocaleProvider} from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

class MeetTopicRemarkDialog extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      remark: props.remark
    };
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.remark != this.props.remark) {
      this.setState({
        remark: nextProps.remark
      });
    }
  }
  
  br2nl = (txt) => {
    if(!txt) return '';
    var re=/(<br\/>|<br>|<BR>|<BR\/>)/g;
    var s=txt.replace(re,"\n");
    return s;  
  }

  render() {
    let {visible, remark} = this.state;
    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@nnwe3c`} visible={visible} title={getLabel(454, "备注")}
                 icon="icon-coms-meeting" iconBgcolor="#f14a2d"
                 onCancel={this.back}
                 style={{height: 200}}
                 buttons={[
                   <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@78fpd0@save`} type="primary" onClick={this.save}>{getLabel(383323, "保存")}</Button>,
                   <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@wy63cg@goBack`} onClick={this.back}>{getLabel(130339, "返回")}</Button>,
                 ]}>
        <div className="wea-meet-member-remark">
          <div className="ant-col-6">{`${getLabel(454, "备注")}：`}</div>
          <div className="ant-col-18">
            <Input ecId={`${this && this.props && this.props.ecId || ''}_Input@xg1lts`} type="textarea" placeholder={getLabel(383556, "在此输入内容")} autosize={{minRows: 8, maxRows: 8}}
                   onChange={(e) => this.setState({remark: e.target.value})} value={this.br2nl(remark)}/>
          </div>
        </div>
      </WeaDialog>
    );
  }

  open = () => {
    this.setState({
      visible: true
    });
  }

  back = () => {
    this.setState({
      visible: false,
    });
  }

  save = () => {
    let remark = this.state.remark || '';
    if(remark.split(' ').join('').length == 0){//备注必填校验
      Modal.error({
        title: getLabel(15172,'系统提示'),
        content: getLabel(386920,'请输入备注'),
      });
    }else{
      let _this = this;
      Apis.saveRemark({meetingid: this.props.meetingId, othersremark: remark}).then((result) => {
        this.back();
        Apis.getMeetingRemark({meetingid: this.props.meetingId}).then(res => {
          if(res && res.status) {
            _this.props.setNewRemark && _this.props.setNewRemark(res.othersremark);
          }
        });
      });
    }
  }
}

export default MeetTopicRemarkDialog;